package com.two.shopping.controller;

import com.two.shopping.pojo.Commodity;
import com.two.shopping.pojo.Result;
import com.two.shopping.service.IAdminService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "http://localhost:8000")
@Slf4j
@RestController
public class AdminController {
    @Autowired
    private IAdminService adminService;

    @CrossOrigin
    @GetMapping("/admin/list")
    private Result allList(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "5") Integer pageSize) {
        log.info("这是管理员控制层page{},pageSize{}", page, pageSize);
        return Result.success(adminService.allList(page, pageSize));
    }

    @CrossOrigin(origins = "http://localhost:8000")
    @DeleteMapping("/admin/delete")
    private Result deleteId(Integer id) {
        adminService.delete(id);
        return Result.success("成功");
    }

    @CrossOrigin(origins = "http://localhost:8000")
    @PostMapping("/admin/deletes/{idsString}")
    private Result deleteIds(@PathVariable List<Integer> idsString) {
        adminService.deletes(idsString);
        return Result.success("成功");
    }

    @CrossOrigin
    @GetMapping("/admin/listLike")
    private Result allListLike(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "5") Integer pageSize,
                               @RequestParam(defaultValue = "") String name,
                               @RequestParam(defaultValue = "") Integer state,
                               @RequestParam(defaultValue = "") Integer commodityTypeId) {
        log.info("这是管理员控制层page{},pageSize{}", name, state, commodityTypeId);
        return Result.success(adminService.pagelike(page, pageSize, name, state, commodityTypeId));
    }

    @CrossOrigin
    @PostMapping("/admin/update")
    private Result update(@RequestBody Commodity commodity) {
        log.info("{}",commodity);
        adminService.update(commodity);
        return Result.success("成功修改");
    }

    @CrossOrigin
    @PostMapping("/admin/add")
    private Result add(@RequestBody Commodity commodity) {
        log.info("{}",commodity);
        adminService.add(commodity);
        return Result.success("成功添加");
    }
}
